﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MCEMediaManager
{
    public class ApplicationDataStore
    {
        private DataStoreHashTable _data;
        public ApplicationDataStore()
        {
            _data = new DataStoreHashTable(Application.Current.SharedDirectory + "MCEMediaManager_AppStore");
        }

        public object GetObject(string key, object defaultValue)
        {
            try
            {
                defaultValue = Application.Current.RootRegKey.GetValue(key, defaultValue);
            }
            catch
            {
                //continue
            }
            try
            {
                if (_data.ContainsKey(key))
                {
                    defaultValue = _data[key];
                }
            }
            catch
            {
            }
            return defaultValue;
        }

        public string GetString(string key, string defaultValue)
        {
            return (string)GetObject(key, (object)defaultValue);
        }

        public int GetInt(string key, int defaultValue)
        {
            return (int)GetObject(key, (object)defaultValue);
        }

        public bool SetValue(object key, object value)
        {
            bool returnValue = true;

            try
            {
                if (_data.Contains(key))
                {
                    _data[key] = value;
                }
                else
                {
                    _data.Add(key, value);
                }

                _data.IsDirty = true;
                _data.Save();
            }
            catch(Exception ex)
            {
                returnValue = false;
            }
            return returnValue;
        }
    }
}
